package net.wsfcp.android.ui.view.my.fragment;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseFragment;
import net.wsfcp.android.bean.requestbean.ReqCityBean;
import net.wsfcp.android.bean.requestbean.ReqCreateAddressBean;
import net.wsfcp.android.bean.resultbean.AddressBean;
import net.wsfcp.android.bean.resultbean.CityBean;
import net.wsfcp.android.http.mvp.contract.AddressContract;
import net.wsfcp.android.event.AddressEvent;
import net.wsfcp.android.http.mvp.presenter.CreateAddressPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//添加地址子页面
public class AddAddressFragment extends BaseFragment<CreateAddressPresenter> implements AddressContract.CreateAddressView {
    @BindView(R.id.txt_save)
    TextView txtSave;
    @BindView(R.id.edt_address_receive)
    EditText edtAddressReceive;
    @BindView(R.id.txt_address_city)
    TextView txtAddressCity;
    @BindView(R.id.edt_address_detail)
    EditText edtAddressDetail;
    @BindView(R.id.edt_address_phone)
    EditText edtAddressPhone;

    //省市区列表
    List<CityBean> provinceBeans = new ArrayList<CityBean>();
    List<CityBean> cityBeans = new ArrayList<CityBean>();
    List<CityBean> districtBeans = new ArrayList<CityBean>();

    //省市区名称列表
    List<String> provinceStr = new ArrayList<String>();
    List<String> cityStr = new ArrayList<String>();
    List<String> districtStr = new ArrayList<String>();


    int oldProvince = 0;
    int oldCity = 0;
    int oldDistrict = 0;

    //地址选择器
    OptionsPickerView pvOptions;

    String myProvince = "";//选中的省
    String myCity = "";//选中的市
    String myDistrict = "";//选中的区

    boolean isEdit;//是否是编辑已有的地址
    String edtid = "";//正在编辑的地址id

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_address;
    }

    @Override
    protected CreateAddressPresenter loadPresenter() {
        return new CreateAddressPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        txtSave.setOnClickListener(this);
        txtAddressCity.setOnClickListener(this);
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.txt_save:
                ReqCreateAddressBean bean = new ReqCreateAddressBean();
                bean.setReceiver(edtAddressReceive.getText().toString().trim());
                bean.setCellNum(edtAddressPhone.getText().toString().trim());
                bean.setDetailAddress(edtAddressDetail.getText().toString().trim());
                if (!TextUtils.isEmpty(myProvince)) {
                    bean.setProvince(myProvince);
                }
                if (!TextUtils.isEmpty(myCity)) {
                    bean.setCity(myCity);
                }
                if (!TextUtils.isEmpty(myDistrict)) {
                    bean.setArea(myDistrict);
                }

                if (isEdit) {
                    bean.setId(edtid);
                }

                mPresenter.createAddress(bean);
                break;


            case R.id.txt_address_city:
                //条件选择器
                hideSoftKeyboard();
                getProvince();

                break;
        }

    }

    //获取省列表
    public void getProvince() {
        mPresenter.getCity(new ReqCityBean(2));
    }

    //获取市列表
    public void getCity(String parentName) {
        mPresenter.getCity(new ReqCityBean(3, parentName));
    }

    //获取区列表
    public void getDistrict(String parentName) {
        mPresenter.getCity(new ReqCityBean(4, parentName));
    }


    public void initCity() {
        if (pvOptions == null) {
            pvOptions = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    //返回的分别是三个级别的选中位置

                    myProvince = provinceStr.size() > options1 ? provinceStr.get(options1) : "";
                    myCity = cityStr.size() > option2 ? cityStr.get(option2) : "";
                    myDistrict = districtStr.size() > options3 ? districtStr.get(options3) : "";

                    String tx = myProvince + " " + myCity + " " + myDistrict;


                    txtAddressCity.setText(tx);
                }
            }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
                @Override
                public void onOptionsSelectChanged(int options1, int options2, int options3) {

                    if (oldProvince != options1) {
                        oldProvince = options1;

                        getCity(provinceStr.get(oldProvince));
                    }
                    if (oldCity != options2) {
                        oldCity = options2;
                        getDistrict(cityStr.get(oldCity));
                    }
                    if (oldDistrict != options3) {
                        oldDistrict = options3;
                    }


                }
            }).build();
        }
//        pvOptions.setPicker(cityBeans, cityBeans, cityBeans);//联动
        pvOptions.setNPicker(provinceStr, cityStr, districtStr);//不联动
        pvOptions.setSelectOptions(oldProvince, oldCity);
        if (!pvOptions.isShowing())
            pvOptions.show();
    }


    public boolean checkNull() {
        boolean isNull = false;
        if (edtAddressReceive.getText().toString().trim().length() == 0) {
            toast("收件人不能为空");
            isNull = true;
        } else if (txtAddressCity.getText().toString().trim().length() == 0) {
            toast("城市不能为空");
            isNull = true;
        } else if (edtAddressDetail.getText().toString().trim().length() == 0) {
            toast("详细地址不能为空");
            isNull = true;
        } else if (edtAddressPhone.getText().toString().trim().length() == 0) {
            toast("手机号不能为空");
            isNull = true;
        }
        return isNull;
    }

    @Override
    public String getOldPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRepeatNewPwd() {
        return null;
    }

    @Override
    public void createAddressSuccess() {
        EventBus.getDefault().post(new AddressEvent(1));
        toast("保存成功");
    }

    @Override
    public void createAddressFail(String failMsg) {
        toast(failMsg);

    }

    @Override
    public void getCitySuccess(ArrayList<CityBean> beans) {

        int type = 0;
        if (beans.size() > 0) {
            type = beans.get(0).getLevel();
        }


        switch (type) {
            case 2://省列表
                Log.e("获取列表", "获取省");

                provinceBeans.clear();
                provinceBeans.addAll(beans);

                provinceStr.clear();
                for (int i = 0; i < provinceBeans.size(); i++) {
                    provinceStr.add(provinceBeans.get(i).getName());
                }

                if (provinceStr.size() > 0) {
                    oldProvince = 0;
                    getCity(provinceStr.get(0));
                }


                break;

            case 3://市列表
                Log.e("获取列表", "获取市");
                cityBeans.clear();
                cityBeans.addAll(beans);

                cityStr.clear();
                for (int i = 0; i < cityBeans.size(); i++) {
                    cityStr.add(cityBeans.get(i).getName());
                }

                if (cityStr.size() > 0) {
                    oldCity = 0;
                    getDistrict(cityStr.get(0));
                }

                break;
            case 4://区列表
                Log.e("获取列表", "获取区");
                districtBeans.clear();
                districtBeans.addAll(beans);

                districtStr.clear();
                for (int i = 0; i < districtBeans.size(); i++) {
                    districtStr.add(districtBeans.get(i).getName());
                }
                initCity();
                break;
        }


    }

    @Override
    public void getCityFail(String failMsg, int type) {
        switch (type) {
            case 2:
                provinceBeans.clear();
                provinceStr.clear();
                cityBeans.clear();
                cityStr.clear();
                districtBeans.clear();
                districtStr.clear();
                initCity();
                break;
            case 3:

                cityBeans.clear();
                cityStr.clear();
                districtBeans.clear();
                districtStr.clear();
                initCity();

                break;
            case 4:

                districtBeans.clear();
                districtStr.clear();
                initCity();
                break;

        }

    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(AddressEvent event) {
        switch (event.getCode()) {
            case 0:   // 删除

                break;
            case 1:   // 新增
                edtAddressReceive.setText("");
                txtAddressCity.setText("");
                edtAddressDetail.setText("");
                edtAddressPhone.setText("");
                myProvince = "";
                myCity = "";
                myDistrict = "";
                break;
            case 2:   // 修改
                isEdit = true;
                AddressBean myAddressbean = event.getAddressBean();
                if (myAddressbean != null) {
                    if (!TextUtils.isEmpty(myAddressbean.getProvince())) {
                        myProvince = myAddressbean.getProvince();
                    }
                    if (!TextUtils.isEmpty(myAddressbean.getCity())) {
                        myCity = myAddressbean.getCity();
                    }
                    if (!TextUtils.isEmpty(myAddressbean.getArea())) {
                        myDistrict = myAddressbean.getArea();
                    }

                    edtid = myAddressbean.getId();
                    edtAddressReceive.setText(myAddressbean.getReceiver());
                    txtAddressCity.setText(myProvince + " " + myCity + " " + myDistrict);
                    edtAddressDetail.setText(myAddressbean.getDetailAddress());
                    edtAddressPhone.setText(myAddressbean.getCellNum());

                }


                break;

            case 3:   // 手动创建
                isEdit = false;
                myProvince = "";
                myCity = "";
                myDistrict = "";

                edtAddressReceive.setText("");
                txtAddressCity.setText("");
                edtAddressDetail.setText("");
                edtAddressPhone.setText("");
                break;


        }

    }


    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyboard() {

        InputMethodManager inputMethodManager =
                (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


}
